<template>
  <el-dialog
    title="新增目录"
    :visible.sync="showDialog"
    width="30%"
    :before-close="handlereset"
  >
    <el-form :rules="rules" :model="ruleForm" ref="formName">
      <el-form-item label="所属学科">
        <el-select
          autocomplete="off"
          style="width: 340px"
          placeholder="请选择"
          v-model="ruleForm.subjectID"
        >
          <el-option
            v-for="item in subjectList"
            :key="item.id"
            :label="item.subjectName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="tagName">
        <el-input
          autocomplete="off"
          style="width: 330px"
          placeholder="请输入目录名称"
          v-model="ruleForm.tagName"
          :required="true"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancle">取 消</el-button>
      <el-button type="primary" @click="confirm(ruleForm)">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { list } from "@/api/hmmm/subjects";
import { add } from "@/api/hmmm/tags";
export default {
  data() {
    return {
      subject: "",
      subjectList: [],
      ruleForm: {
        tagName: "",
        subjectID: "",
      },
      rules: {
        tagName: [
          { required: true, message: "请输入目录名称", trigger: "blur" },
        ],
      },
    };
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false,
      required: true,
    },
  },
  methods: {
    cancle() {
      this.ruleForm.tagName = "";
      this.ruleForm.subjectID = "";
      this.$emit("change");
    },
    async confirm() {
      try {
        await this.$refs.formName.validate();
        console.log(this.ruleForm);
        const result = await add(this.ruleForm);
        console.log(result);
        this.$emit("change");
        this.$message.success('新增标签成功')
        this.ruleForm.tagName = "";
        this.ruleForm.subjectID = "";
      } catch (err) {}
    },
    handlereset() {
      this.ruleForm.tagName = "";
      this.ruleForm.subjectID = "";
      this.$emit("change");
    },
    async subjectListShow() {
      const results = await list();
      this.subjectList = results.data.items;
      // console.log(this.subjectList);
    },
  },
  created() {
    this.subjectListShow();
  },
};
</script>

<style scoped lang='less'></style>
